package sorts;

public class offers_45 {
    public String minNumber(int[] nums) {
        String[] strs = new String[nums.length];

        for (int i = 0; i < nums.length; i++) {
            strs[i] = String.valueOf(nums[i]);
        }

        quickSort(strs, 0, strs.length - 1);

        StringBuffer buffer = new StringBuffer();

        for (String item : strs) {
            buffer.append(item);
        }
        return buffer.toString();
    }

    private void quickSort(String[] arr, int left, int right) {
        if (left >= right) return;
        int l = left;
        int r = right;
        String key = arr[l];
        while (l < r) {
            while (l < r && (arr[r] + arr[left]).compareTo(arr[left] + arr[r]) > 0) r--;
            while (l < r && (arr[l] + arr[left]).compareTo(arr[left] + arr[l]) <= 0) l++;
            key = arr[l];
            arr[l] = arr[r];
            arr[r] = key;
        }
        arr[l] = arr[left];
        arr[left] = key;
        quickSort(arr, left, l - 1);
        quickSort(arr, l + 1, right);
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 1};
        offers_45 f = new offers_45();
        System.out.println(f.minNumber(arr));

    }
}
